package br.com.aerusc.gui.panel;

import java.util.logging.Logger;

import br.com.aerusc.entity.Associado;
import br.com.aerusc.gui.components.RootComponents;
import br.com.aerusc.gui.forms.AssociadoForm;
import br.com.aerusc.persistence.dao.AssociadoDAO;

import com.vaadin.data.util.BeanItem;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;

public class PanelCadastroAssociados extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	private GridLayout gridLayout;
	private Associado associado;
	private Form associadoForm;
	private Window windowInformacoes;
	private Button buttonSalvar;
	private Button buttonLimpar;
	private Button buttonDeletar;
	private static final Logger log = Logger.getLogger(PanelCadastroAssociados.class.getName());
	
	public PanelCadastroAssociados(){
		super();
		associado = new Associado();
		preencherForm();
		gridLayout = new GridLayout(3, 1);
		gridLayout.addComponent(getButtonSalvar(),0,0);
		gridLayout.addComponent(getButtonLimpar(),1,0);
		addComponent(associadoForm);
		addComponent(gridLayout);
	}
	
	
	public PanelCadastroAssociados(Associado entity){
		super();
		associado = entity;
		preencherForm();
		gridLayout = new GridLayout(3, 1);
		gridLayout.addComponent(getButtonSalvar(),0,0);
		gridLayout.addComponent(getButtonLimpar(),1,0);
		gridLayout.addComponent(getButtonDeletar(),2,0);
		addComponent(associadoForm);
		addComponent(gridLayout);
	}
	
	public void preencherForm(){
		BeanItem<Associado> associadoItem = new BeanItem<Associado>(associado);
		associadoForm = new AssociadoForm(associadoItem);
		associadoForm.setImmediate(true);
		associadoForm.setWriteThrough(false);
		
	}
	
	public Button getButtonSalvar() {
		buttonSalvar = new Button("Salvar", new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					associadoForm.commit();
				} catch (Exception e) {
					log.warning(e.getMessage());
					return;
				}
				AssociadoDAO dao = new AssociadoDAO();
				associado = dao.salvar(associado);
				if (associado != null) {
					RootComponents.getMainWindow().addWindow(getWindowInformacoes("Associado Salvo com Sucesso!"));
				}

			}

		});
		buttonSalvar.setClickShortcut(KeyCode.ENTER);
		return buttonSalvar;
	}

	public Button getButtonLimpar() {
		buttonLimpar = new Button("Limpar", new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					associadoForm.discard();
				} catch (Exception e) {
					log.warning(e.getMessage());
					return;
				}
			}

		});
		return buttonLimpar;
	}

	public Button getButtonDeletar() {
		buttonDeletar = new Button("Deletar", new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				AssociadoDAO dao = new AssociadoDAO();
				dao.remover(associado);
				RootComponents.getMainWindow().addWindow(getWindowInformacoes("Associado Removido com Sucesso !"));

			}

		});
		return buttonDeletar;
	}

	public Window getWindowInformacoes(String label) {
		windowInformacoes = new Window("Sucesso");
		windowInformacoes.addComponent(new Label(label));
		windowInformacoes.setWidth("200px");
		windowInformacoes.setHeight("100px");
		windowInformacoes.setModal(true);
		windowInformacoes.setClosable(false);
		Button buttonConfirmar = new Button("Confirmar", new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					windowInformacoes.setVisible(false);
					windowInformacoes.detach();
					RootComponents.getHorizontalSplitLayout().setSecondComponent(new PanelCadastroAssociados());
				} catch (Exception e) {
					log.warning(e.getMessage());
					return;
				}
			}

		});
		buttonConfirmar.focus();
		buttonConfirmar.setClickShortcut(KeyCode.ENTER);
		windowInformacoes.addComponent(buttonConfirmar);

		return windowInformacoes;
	}

	
}
